package com.weyoung.app.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.weyoung.app.common.convention.result.Result;
import com.weyoung.app.pojo.dto.req.UserLoginReqDTO;
import com.weyoung.app.pojo.dto.req.UserUpdateReqDTO;
import com.weyoung.app.pojo.dto.resp.UserDetailRespDTO;
import com.weyoung.app.pojo.dto.resp.UserLoginRespDTO;
import com.weyoung.app.service.UserService;
import com.weyoung.app.utils.WechatUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     *  微信登录
     *
     * @param code 登录凭证
     * @param rawData 用户信息
     * @param signature 签名
     * @return 用户信息
     */
    @PostMapping(value = "/wx/login")
    public Result<UserDetailRespDTO> WXLogin(@RequestParam(value = "code", required = false) String code,
                                            @RequestParam(value = "rawData", required = false) String rawData,
                                            @RequestParam(value = "signature", required = false) String signature){
        // 用户非敏感信息：rawData
        // 签名：signature
        JSONObject rawDataJson = JSON.parseObject(rawData);
        // 1.接收小程序发送的code
        // 2.开发者服务器 登录凭证校验接口 appi + appsecret + code
        JSONObject SessionKeyOpenId = WechatUtil.getSessionKeyOrOpenId(code);
        // 3.接收微信接口服务 获取返回的参数
        String openid = SessionKeyOpenId.getString("openid");
        String sessionKey = SessionKeyOpenId.getString("session_key");

        // 4.校验签名 小程序发送的签名signature与服务器端生成的签名signature2 = sha1(rawData + sessionKey)
        String signature2 = DigestUtils.sha1Hex(rawData + sessionKey);
        if (!signature.equals(signature2)) {
            throw new RuntimeException("签名校验失败");
        }
        UserDetailRespDTO userLoginRespDTO = userService.WXLogin(openid,rawDataJson.getString("nickName"),rawDataJson.getString("avatarUrl"));
        return Result.success(userLoginRespDTO);
    }

    /**
     * 使用用户名密码登录
     *
     * @param requestPram 请求参数
     * @return 用户信息
     */
    @PostMapping(value = "/login")
    public Result<UserDetailRespDTO> login(@RequestBody UserLoginReqDTO requestPram){
        return Result.success(userService.login(requestPram));
    }

    /**
     * 修改信息
     */
    @PostMapping(value = "/update")
    public Result<UserDetailRespDTO> update(@RequestBody UserUpdateReqDTO requestPram){
        return Result.success(userService.update(requestPram));
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping(value = "/userDetail")
    public Result<UserDetailRespDTO> userDetail(){
        return Result.success(userService.getUserDetail());
    }

    @GetMapping("/loginout")
    public Result<String> loginout(){
        StpUtil.logout();
        return Result.success("退出成功");
    }


}
 